﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UseInfoPanel : UIBase
{
    /// <summary>  
    /// 第一次按下的位置  
    /// </summary>  
    private Vector2 first = Vector2.zero;
    /// <summary>  
    /// 鼠标的拖拽位置（第二次的位置）  
    /// </summary>  
    private Vector2 second = Vector2.zero;

    GameObject m_Detail;
    GameObject m_Sliding_node;

    Dictionary<string, Actor> m_actors;
    RawImage m_rawImage;

    Actor LocalActor;
    Image ShutDownImage;

    /// <summary>
    /// 模型间距
    /// </summary>
    int m_modelToSpacing = 6;
    
    Button backBtn;
    Button RefreshBtn;
    void Awake()
    {
        GameObject go = new GameObject("UseNode");
        m_rawImage = transform.Find("RawImage").GetComponent<RawImage>();
        m_Detail = transform.Find("Detail").gameObject;
        m_Detail.SetActive(false);
        m_Sliding_node = GameObject.Find("UseNode");

        //ShutDownImage = this.GetComponent<Image>();
        //ShutDownImage.enabled = false;

        UpdateTheUserData();

        backBtn = transform.Find("BackBtn").GetComponent<Button>();
        RefreshBtn = transform.Find("Refresh_btn").GetComponent<Button>();
        backBtn.onClick.AddListener(OnResetThePosition);
        RefreshBtn.onClick.AddListener(() => { OnResetOtherUsersInfo(); });
    }

    // 刷新按钮事件
    public void OnResetOtherUsersInfo()
    {
        Debug.LogFormat("<color=red>刷新人物列表</color>");
        UpdateTheUserData();
    }


    public void OnResetThePosition()
    {
        Debug.LogFormat("<color=red>重置位置</color>");
        //m_Sliding_node.transform.position = new Vector3(-3, 0, -5);
        //m_Sliding_node.transform.position = Vector3.Lerp(m_Sliding_node.transform.position, new Vector3(-3, 0, -5), 0.5f);
    }
    List<Actor> m_acList = new List<Actor>();
    float Pos_x = 0;
    public void UpdateTheUserData()
    {
        if (ActorManager.Instance.GetActors().Count == 1)
        {
            Debug.LogFormat("<color=red>是自己，未有新玩家</color>");
            m_rawImage.gameObject.SetActive(false);
        }
        else
        {
            Debug.LogFormat("<color=red>有新玩家</color>");
            m_actors = ActorManager.Instance.GetActors();
            //m_Sliding_node.transform.localPosition = new Vector3(-3, -5, 0);
            RenderTool.Instance.AddRenderTex(m_rawImage, m_Sliding_node);
            m_Sliding_node.transform.position = new Vector3(0, -7, 2);
            foreach (KeyValuePair<string, Actor> kvp in m_actors)
            {       
                if (kvp.Key != ActorManager.Instance.LocalID)
                {
                    Actor ac = ActorManager.Instance.GetActor(m_actors[kvp.Key].account.id);
                    ac.gameObject.transform.SetParent(m_Sliding_node.transform);
                    ac.gameObject.SetActive(true);
                    //ac.gameObject.transform.localPosition = new Vector3(0, 0, -10);
                    m_acList.Add(ac);
                }
            }
            Debug.LogFormat("<color=red>用户有{0}个！</color>", m_acList.Count);
            for (int i = 0; i < m_acList.Count; i++)
            {
                m_acList[i].gameObject.transform.rotation = new Quaternion(0, 0, 0, 0);
                m_acList[i].gameObject.transform.localScale = new Vector3(3f, 3f, 3f);
                //m_acList[i].gameObject.transform.localPosition = new Vector3(0, -3, -5);
                Vector3 numPos = m_acList[i].gameObject.transform.position;
                numPos.x = Pos_x;
                m_acList[i].gameObject.transform.position = numPos;
                Pos_x += m_modelToSpacing;
            }
        }
    }

    #region  滑动方法一
    //void OnGUI()
    //{
    //    if (Event.current.type == EventType.MouseDown)
    //    {
    //        //记录鼠标按下的位置 　　  
    //        first = Event.current.mousePosition;
    //    }
    //    if (Event.current.type == EventType.MouseDrag)
    //    {
    //        //记录鼠标拖动的位置 　　  
    //        second = Event.current.mousePosition;

    //        if (second.x > first.x)
    //        {
    //            //拖动的位置的x坐标比按下的位置的x坐标小时
    //            //this.transform.Rotate(Vector3.up, angle);
    //            Debug.LogFormat("<color=red>向右</color>");
    //            //m_Sliding_node.transform.localPosition.x += 3;
    //            //Vector3 dir = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y, 0f) * 0.1f;

    //            m_Sliding_node.transform.Translate(Vector3.left * Time.deltaTime * 24f);
    //        }
    //        if (second.x < first.x)
    //        {
    //            //拖动的位置的x坐标比按下的位置的x坐标大时
    //            //this.transform.Rotate(Vector3.down, angle);
    //            Debug.LogFormat("<color=red>向左</color>");
    //            m_Sliding_node.transform.Translate(Vector3.right * Time.deltaTime * 24f);
    //        }
    //        first = second;
    //    }
    //}
    #endregion


    #region  滑动方法二
    enum slideVector { nullVector, left, right };

    private Vector2 lastPos;//上一个位置

    private Vector2 currentPos;//下一个位置

    private slideVector currentVector = slideVector.nullVector;//当前滑动方向

    private float timer;//时间计数器

    public float offsetTime = 0.01f;//判断的时间间隔


    void OnGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {//滑动开始
            lastPos = Event.current.mousePosition;
            currentPos = Event.current.mousePosition;
            timer = 0;

            //TODO click event
            Debug.Log("Click begin && Drag begin");
        }

        if (Event.current.type == EventType.MouseDrag)
        {//滑动过程
            currentPos = Event.current.mousePosition;
            timer += Time.deltaTime;
            if (timer > offsetTime)
            {
                if (currentPos.x < lastPos.x)
                {
                    //if (currentVector == slideVector.left)
                    //{
                    //    return;
                    //}
                    //TODO trun Left event

                    currentVector = slideVector.left;
                    Debug.Log("Turn left");

                    m_Sliding_node.transform.Translate(Vector3.right * Time.deltaTime * 12f);
                }
                if (currentPos.x > lastPos.x)
                {
                    //if (currentVector == slideVector.right)
                    //{
                    //    return;
                    //}
                    //TODO trun right event

                    currentVector = slideVector.right;
                    Debug.Log("Turn right");
                    m_Sliding_node.transform.Translate(Vector3.left * Time.deltaTime * 12f);
                }

                lastPos = currentPos;
                timer = 0;
            }
        }

        if (Event.current.type == EventType.MouseUp)
        {//滑动结束
            currentVector = slideVector.nullVector;
            Debug.Log("Click over && Drag over");
        }
    }
    #endregion


    Vector3 startPos;
    Vector3 endPos;
    float LerpSpeed = 5f;
    int UpdateNumber = 0;
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPos = m_Sliding_node.transform.position;
            UpdateNumber = 0;
        }
        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("鼠标抬起位置" + endPos.ToString());
            UpdateNumber = 1;
            endPos = m_Sliding_node.transform.position;

            float xx;
            if (endPos.x >= 0) //向右
            {
                if (endPos.x % m_modelToSpacing < m_modelToSpacing/2)
                {
                    Debug.Log("1:" + (endPos.x % m_modelToSpacing).ToString());
                    xx = endPos.x - endPos.x % m_modelToSpacing;
                }
                else
                {
                    Debug.Log("2:" + (endPos.x % m_modelToSpacing).ToString());
                    xx = endPos.x + (m_modelToSpacing - endPos.x % m_modelToSpacing);
                }
            }
            else //向左
            {
                if (Mathf.Abs(endPos.x % m_modelToSpacing) < m_modelToSpacing / 2)
                {
                    Debug.Log("3:" + (endPos.x % m_modelToSpacing).ToString());
                    //xx = endPos.x + Mathf.Abs(endPos.x % 3);
                    xx = endPos.x - Mathf.Abs(endPos.x % m_modelToSpacing);
                }
                else
                {
                    Debug.Log("4:" + (endPos.x % m_modelToSpacing).ToString());
                    xx = endPos.x - (m_modelToSpacing - Mathf.Abs(endPos.x % m_modelToSpacing));
                }
            }
            endPos.x = xx;
            

        }
        if(UpdateNumber == 1)
        {
            Vector3 v3 = new Vector3(endPos.x, m_Sliding_node.transform.position.y, m_Sliding_node.transform.position.z);
            m_Sliding_node.transform.position = Vector3.Lerp(m_Sliding_node.transform.position, v3, 1f * Time.deltaTime * LerpSpeed);
        }


    }

    public override void OnClose(params object[] args)
    {
        RenderTool.Instance.Close();
    }
}